<template>
    <!-- 对话框表单 -->
    <el-dialog custom-class="ba-operate-dialog" :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form v-if="!baTable.form.loading" ref="formRef" @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items" label-position="right" :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules">
                    <el-form-item :label="t('feedback.user_id')">
                        <el-input :value="baTable.form.items!.user_id" readonly />
                    </el-form-item>
                    <el-form-item :label="t('feedback.desc')">
                        <el-input :value="baTable.form.items!.desc" readonly />
                    </el-form-item>
                    <el-form-item label="">
                        <el-image v-for="src in baTable.form.items!.images" :src="src"
                            style="width: 100px; height: 100px; margin-right: 10px; cursor: pointer;"
                            @click="toShowSlider(baTable.form.items!.images)" fit="cover" />
                    </el-form-item>
                    <el-form-item :label="t('feedback.mobile')">
                        <el-input :value="baTable.form.items!.mobile" readonly />
                    </el-form-item>
                    <el-form-item :label="t('feedback.username')">
                        <el-input :value="baTable.form.items!.username" readonly />
                    </el-form-item>
                    <FormItem :label="t('feedback.state')" type="radio" v-model="baTable.form.items!.state" prop="state"
                        :data="{ content: { 0: t('feedback.state 0'), 1: t('feedback.state 1') } }"
                        :input-attr="{ placeholder: t('Please select field', { field: t('feedback.state') }) }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
                            t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    user_id: [buildValidatorData('required', t('feedback.user_id'))],
    desc: [buildValidatorData('required', t('feedback.desc'))],
    images: [buildValidatorData('required', t('feedback.images'))],
    mobile: [buildValidatorData('required', t('feedback.mobile')), buildValidatorData('mobile', '', 'blur', t('Please enter the correct field', { field: t('feedback.mobile') }))],
    username: [buildValidatorData('required', t('feedback.username'))],
    state: [buildValidatorData('required', t('feedback.state'))],
})

</script>

<style scoped lang="scss">
</style>
